package by.alex.dzeshko.facebooksuperclient.database;

import org.json.JSONObject;
import by.alex.dzeshko.facebooksuperclient.R;
import by.alex.dzeshko.facebooksuperclient.api.ApiRequest;
import by.alex.dzeshko.facebooksuperclient.models.FbUser;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class DbFriendsHelper extends DbManager{
	
	public DbFriendsHelper(Context context, CursorFactory factory) {
		super(context, factory);
	}
	
	public Cursor getItems() {
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query(DbManager.FRIENDS_TABLE, null,	null, null, null, null,	null);
		return cursor;
	}

	public long deleteTable() {
		SQLiteDatabase db = getWritableDatabase();
		return db.delete(FRIENDS_TABLE, null, null);
	}

	@Override
	public long addItem(JSONObject jsonObject) {
		SQLiteDatabase db = getWritableDatabase();
		ContentValues values = this.getContentValues(jsonObject);
		long value;
		value = db.insertWithOnConflict(DbManager.FRIENDS_TABLE, null, values,SQLiteDatabase.CONFLICT_REPLACE);
		return value; 
	}

	@Override
	protected ContentValues getContentValues(JSONObject pItem) {
		ContentValues values = new ContentValues();
		FbUser user = new FbUser(pItem);
		values.put(DbManager.FRIENDS_COLUMNS[DbManager.FRIEND_NAME_COL_INDEX], user.getName());
		values.put(DbManager.FRIENDS_COLUMNS[DbManager.ID_COL_INDEX], user.getId());
		String id = String.valueOf(user.getId());
		values.put(FRIENDS_COLUMNS[FRIEND_PICTURE_URL_COL_INDEX], ApiRequest.getProfilePicture(id, ApiRequest.SQUARE));
		
		return values;
	}
}



